package com.jzr.bedside.ui.skill

import  android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.utils.ToastUtils
import com.google.gson.Gson
import com.jzr.bedside.R
import com.jzr.bedside.base.BaseActivity
import com.jzr.bedside.bean.CheckedListBean
import com.jzr.bedside.bean.TQuestionBean
import com.jzr.bedside.bean.body.AddSatisfyBean
import com.jzr.bedside.presenter.contract.EvaluationActivityContract
import com.jzr.bedside.presenter.impl.EvaluationActivityImpl
import com.jzr.bedside.ui.adapter.EvaluateApadter
import com.jzr.bedside.utils.PreferUtil
import kotlinx.android.synthetic.main.actionbar_root_layout.*
import kotlinx.android.synthetic.main.activity_evaluation.*
import okhttp3.MediaType
import okhttp3.RequestBody

class EvaluationActivity : BaseActivity(), View.OnClickListener, EvaluationActivityContract.View {

    private val mPresenter = EvaluationActivityImpl()

    private val data =
        mutableListOf<TQuestionBean.DataBean>()

    private var adapter: EvaluateApadter? = null

    override fun getLayoutId(): Int {
        return R.layout.activity_evaluation
    }

    override fun attachView() {
        mPresenter.attachView(this)
    }

    override fun detachView() {
        mPresenter.detachView()
    }

    override fun initView() {
        ivRight.setOnClickListener(this)
        btnPost.setOnClickListener(this)
//        initData()
        mPresenter.getSatisfySurveyList(this)
        adapter = EvaluateApadter(data)
        rvEvaluate.adapter = adapter
        val manage = LinearLayoutManager(this)
        manage.orientation = LinearLayoutManager.VERTICAL
        rvEvaluate.layoutManager = manage

    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.ivRight -> {
                finish()
            }
            R.id.btnPost -> {
                var isbeak = false
                val checkedList: List<CheckedListBean?>? =
                    adapter?.getCheckedList()
                for (i in checkedList!!.indices) {
                    if (checkedList[i]?.answers?.answer == null) {
                        ToastUtils.showLongToast("请在  " + checkedList[i]?.title + "  中选择答案")
                        isbeak = true
                        break
                    }
                }
                if (!isbeak) {
                    val body = AddSatisfyBean()
                    body.bedCode = PreferUtil.getInstance().bedCode
                    body.deptCode = PreferUtil.getInstance().deptCode
                    body.patientId = PreferUtil.getInstance().patientId
                    val listOptionsBean = mutableListOf<AddSatisfyBean.OptionsBean>()
                    for (item in checkedList) {
                        val optionsBean = AddSatisfyBean.OptionsBean()
                        optionsBean.controlId = item?.titleId!!
                        optionsBean.optionId = item.answers?.id!!
                        optionsBean.optionValue = item.answers.answer
                        listOptionsBean.add(optionsBean)
                    }
                    body.options = listOptionsBean
                    val JSON = MediaType.parse("application/json; charset=utf-8")
                    val req =
                        RequestBody.create(JSON, Gson().toJson(body).toString())
                    mPresenter.getBedsideSatisfySurveyAdd(this, req)
                }
            }
        }
    }

    override fun getSatisfySurveyListSuccess(data: TQuestionBean?) {
        adapter?.setNewData(data?.data)
        adapter?.notifyDataSetChanged()
    }

    override fun getBedsideSatisfySurveyAddSuccess(data: TQuestionBean?) {
        ToastUtils.showShortToast("评价成功")
        this.finish()
    }

    override fun showError(message: String?) {
        ToastUtils.showShortToast(message)
    }

}
